package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.technology.entity.MassProductionReview;
import com.mxpio.erp.common.technology.service.MassProductionReviewService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "MassProductionReviewController", description = "产品批产评审申请单")
@RestController
@RequestMapping("/erp/common/item/mass_prod_review/")
public class MassProductionReviewController {

	@Autowired
	private MassProductionReviewService service;

	@GetMapping("list")
	@Operation(summary = "产品批产评审申请单列表", description = "获取产品批产评审申请单列表", method = "GET")
	public Result<List<MassProductionReview>> list(Criteria criteria)  {
		List<MassProductionReview> reviews = service.list(MassProductionReview.class, criteria);
		return Result.OK(reviews);
	}

	@GetMapping("page")
	@Operation(summary = "产品批产评审申请单列表", description = "获取产品批产评审申请单列表", method = "GET")
	public Result<Page<MassProductionReview>> page(Criteria criteria, Integer pageSize, Integer pageNo) {
		Pageable page = PageRequest.of(pageNo-1, pageSize);
		Page<MassProductionReview> reviews = service.listPage(MassProductionReview.class, page, criteria);
		return Result.OK(reviews);
	}
	@PostMapping("add")
	@Operation(summary = "新增产品批产评审申请单", description = "新增产品批产评审申请单", method = "POST")
	public Result<MassProductionReview> add(@RequestBody MassProductionReview review) {
		return service.add(review);
	}

	@RequestMapping("edit")
	@Operation(summary = "编辑产品批产评审申请单申请单", description = "编辑产品批产评审申请单申请单")
	public Result<MassProductionReview> edit(@RequestBody MassProductionReview review) {
		service.update(review);
		return Result.OK(review);
	}

	@DeleteMapping("remove/{code}")
	@Operation(summary = "删除 ", description = "删除", method = "DELETE")
	@Transactional(readOnly = false)
	public Result<MassProductionReview> remove(@PathVariable(name = "code", required = true) String code) {
		String[] codes = code.split(",");
		for(String key : codes){
			service.delete(MassProductionReview.class, key);
		}
		return Result.OK();
	}

	@GetMapping("list/{code}")
	@Operation(summary = "根据编码-获取申请单信息", description = "根据编码-获取申请单信息", method = "GET")
	public Result<MassProductionReview> getById(@PathVariable(name = "code", required = true) String code) {
		MassProductionReview review = service.getByIdWith(code);
		return Result.OK(review);
	}

	@GetMapping("audit_pass/{code}/{result}")
	@Operation(summary = "产品批产评审申请单-审核结果回调", description = "产品批产评审申请单-审核结果回调", method = "GET")
	public Result<MassProductionReview> auditPass(@PathVariable(name = "code", required = true) String code,
												  @PathVariable(name = "result", required = true) String result  ) {
		return service.auditPass(code,result);
	}
}
